package org.dentalcard.ui.schedule.tools;

import org.dentalcard.entity.Shift;
import org.dentalcard.entity.WorkDay;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.text.DateFormat;

/**
 * @user: Vladimir Chernishov
 * @date: 25.12.11
 * @time: 19:31
 */
public class WorkScheduleRenderer extends JLabel implements TableCellRenderer {

    private Color[] colors = new Color[]{Color.YELLOW, Color.GREEN, Color.RED, Color.BLUE, Color.ORANGE, Color.MAGENTA};
    private DateFormat smallFormat = DateFormat.getDateInstance();

    public WorkScheduleRenderer() {
        setOpaque(true);
        this.setVerticalAlignment(SwingConstants.TOP);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(value == null || value instanceof String || (value instanceof WorkScheduleDay && ((WorkScheduleDay)value).getDate() == null)){
            setForeground(Color.DARK_GRAY);
            setBackground(Color.DARK_GRAY);
            setText("");
        } else if(value instanceof WorkScheduleDay) {
            WorkScheduleDay day = (WorkScheduleDay) value;
            if (day.getDate() != null) {
                Color bgColor = colors[day.getDate().getMonth() % colors.length];
                setForeground(Color.DARK_GRAY);
                setBackground(bgColor);

                if (day.getDay() != null) {
                    Shift shift = day.getDay().getShift();
                    this.setText("<html>" + smallFormat.format(day.getDate()) + "<br>from " + shift.getFromTime() + " till "
                            + shift.getTillTime() + "<br>" + shift.getName() + "</html>");
                } else {
                    this.setText(smallFormat.format(day.getDate()));

                }

            }else {
                setForeground(Color.DARK_GRAY);
                setBackground(Color.DARK_GRAY);
                setText("");
            }
        }
         
        return this;
    }


}
